package xyz.summer.enchatpoints.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;

import org.bukkit.configuration.file.YamlConfiguration;

import xyz.summer.enchatpoints.EnchantPoints;

public class ConfigurationUtil {

	public static YamlConfiguration LoadYamlConfiguration(String name) {
		try {
			createYamlConfiguration(name);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return YamlConfiguration.loadConfiguration(new File(EnchantPoints
				.getInstance().getDataFolder(), name));
	}

	public static boolean SaveYamlConfiguration(String name,YamlConfiguration yaml) {
		try {
			Writer writer = new OutputStreamWriter(new FileOutputStream(new File(EnchantPoints.getInstance().getDataFolder(),name)));
			writer.write(yaml.saveToString());
			writer.flush();
			writer.close();
		} catch (FileNotFoundException e) {
			return false;
		} catch (IOException e) {
			return false;
		}
		return true;
	}

	public static void createYamlConfiguration(String name) throws IOException {
		File f = new File(EnchantPoints.getInstance().getDataFolder()
				.getAbsolutePath());
		if (!f.exists())
			f.mkdirs();
		f = new File(EnchantPoints.getInstance().getDataFolder(), name);
		if (!f.exists())
			f.createNewFile();
	}

}
